#include "tinyxml2.h"
#include <vector>
#include <string>
#include <iostream>

using namespace tinyxml2;
using namespace std;

// 定义数据结构
struct Source {
    int channel;
    float gain;
};

struct Target {
    int channel;
    float gain;
};

struct Remix {
    vector<Source> sources;
    Target target;
};

struct Delay {
    int duration = 0;
    bool enabled = false;
    vector<int> exceptChannels;
};

struct Remixes {
    bool enabled = false;
    vector<Remix> remixes;
};

struct CrossEffect {
    Delay delay;
    Remixes remixes;
};

CrossEffect parseCrossEffectXML(const string& filename) {
    XMLDocument doc;
    CrossEffect crossEffect;

    if (doc.LoadFile(filename.c_str()) != XML_SUCCESS) {
        cerr << "加载XML文件失败: " << doc.ErrorStr() << endl;
        return crossEffect;
    }

    XMLElement* root = doc.FirstChildElement("cross_effect");
    if (!root) {
        cerr << "找不到根元素 cross_effect" << endl;
        return crossEffect;
    }

    // 解析delay部分
    if (XMLElement* delayElem = root->FirstChildElement("delay")) {
        crossEffect.delay.duration = delayElem->IntAttribute("duration", 0);
        crossEffect.delay.enabled = delayElem->BoolAttribute("enabled", false);

        if (XMLElement* exceptElem = delayElem->FirstChildElement("except")) {
            for (XMLElement* targetElem = exceptElem->FirstChildElement("target");
                 targetElem;
                 targetElem = targetElem->NextSiblingElement("target")) {
                crossEffect.delay.exceptChannels.push_back(
                    targetElem->IntAttribute("channel", -1)
                );
            }
        }
    }

    // 解析remixes部分
    if (XMLElement* remixesElem = root->FirstChildElement("remixes")) {
        crossEffect.remixes.enabled = remixesElem->BoolAttribute("enabled", false);

        for (XMLElement* remixElem = remixesElem->FirstChildElement("remix");
             remixElem;
             remixElem = remixElem->NextSiblingElement("remix")) {
            Remix remix;

            // 解析所有source元素
            for (XMLElement* sourceElem = remixElem->FirstChildElement("source");
                 sourceElem;
                 sourceElem = sourceElem->NextSiblingElement("source")) {
                remix.sources.push_back({
                    sourceElem->IntAttribute("channel", -1),
                    sourceElem->FloatAttribute("gain", 0.0f)
                });
            }

            // 解析target元素
            if (XMLElement* targetElem = remixElem->FirstChildElement("target")) {
                remix.target = {
                    targetElem->IntAttribute("channel", -1),
                    targetElem->FloatAttribute("gain", 0.0f)
                };
                crossEffect.remixes.remixes.push_back(remix);
            } else {
                cerr << "警告: 发现无target的remix元素，已跳过" << endl;
            }
        }
    }

    return crossEffect;
}

// 示例输出函数
void printCrossEffect(const CrossEffect& effect) {
    cout << "=== Delay配置 ===" << endl;
    cout << "持续时间: " << effect.delay.duration << "ms\n启用状态: "
         << (effect.delay.enabled ? "true" : "false") << "\n例外通道: ";
    for (int ch : effect.delay.exceptChannels) cout << ch << " ";
    cout << "\n\n=== Remix配置 ===" << endl;
    cout << "启用状态: " << (effect.remixes.enabled ? "true" : "false") << endl;

    for (size_t i = 0; i < effect.remixes.remixes.size(); ++i) {
        cout << "\nRemix #" << i+1 << ":\n输入源:";
        for (const auto& src : effect.remixes.remixes[i].sources) {
            cout << "\n  通道: " << src.channel << ", 增益: " << src.gain;
        }
        cout << "\n目标: 通道" << effect.remixes.remixes[i].target.channel
             << ", 增益: " << effect.remixes.remixes[i].target.gain << endl;
    }
}

int main() {
    CrossEffect effect = parseCrossEffectXML("cross_effect.xml.prod.v2");
    printCrossEffect(effect);
    return 0;
}
